package com.tjs.lucky.domain.rule.service.logic;


import com.tjs.lucky.common.rule.Global;
import com.tjs.lucky.common.rule.RuleLimitType;
import com.tjs.lucky.domain.rule.mdel.req.DecisionValueReq;
import com.tjs.lucky.domain.rule.mdel.vo.TreeNodeLineVO;

import java.util.List;

/**
 * @Author: 田佳澍
 * @Description:
 * @DateTime: 2023/5/29 16:21
 **/
public abstract class BaseLogic implements LogicFilter {
    @Override
    public Long filter(String decValue, List<TreeNodeLineVO> treeNodeLineInfoList) {
        for (TreeNodeLineVO nodeLine : treeNodeLineInfoList) {
            if (decisionLogic(decValue, nodeLine)) {
                return nodeLine.getNodeIdTo();
            }
        }
        return Global.TREE_NULL_NODE;//0
    }

    /**
     * 获取规则比对值
     *
     * @param valueReq 决策物料
     * @return 比对值
     */
    @Override
    public abstract String getValue(DecisionValueReq valueReq);

    private boolean decisionLogic(String matterValue, TreeNodeLineVO nodeLine) {
        switch (nodeLine.getRuleLimitType()) {
            case RuleLimitType.EQUAL: // =
                return matterValue.equals(nodeLine.getRuleLimitValue());
            case RuleLimitType.GT:  // >
                return Double.parseDouble(matterValue) > Double.parseDouble(nodeLine.getRuleLimitValue());
            case RuleLimitType.LT:  // <
                return Double.parseDouble(matterValue) < Double.parseDouble(nodeLine.getRuleLimitValue());
            case RuleLimitType.GE: // >=
                return Double.parseDouble(matterValue) >= Double.parseDouble(nodeLine.getRuleLimitValue());
            case RuleLimitType.LE: // <=
                return Double.parseDouble(matterValue) <= Double.parseDouble(nodeLine.getRuleLimitValue());
            default:
                return false;
        }
    }
}
